﻿@model IEnumerable<IniTechCaash.WebApplication.Models.FinanceAccountDetailsViewModel>
@using IniTechCaash.Domain.Entities

@{
    ViewBag.Title = "Balance Sheet";

    var currentAssetAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type == AccountType.CurrentAsset || a.Type == AccountType.InventoryAsset);
    var longAssetAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type == AccountType.LongTermAsset || a.Type == AccountType.IntangibleAsset || a.Type == AccountType.EquipmentAsset);
    decimal totalCurrentAssets = 0.00m, totalLongAssets = 0.00m;
    foreach (var a in currentAssetAccounts)
    {
        totalCurrentAssets += a.Balance;
    }
    foreach (var a in longAssetAccounts)
    {
        totalLongAssets += a.Balance;
    }

    var currentLiabilityAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type == AccountType.CurrentLiability);
    var longLiabilityAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type == AccountType.LongTermLiability);
    decimal totalCurrentLiabilities = 0.00m, totalLongLiabilities = 0.00m;
    foreach (var a in currentLiabilityAccounts)
    {
        totalCurrentLiabilities += a.Balance;
    }
    foreach (var a in longLiabilityAccounts)
    {
        totalLongLiabilities += a.Balance;
    }

    var equityAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Equity);
    decimal totalEquities = 0.00m;
    foreach (var a in equityAccounts)
    {
        totalEquities += a.Balance;
    }
    var revenueAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Revenue);
    decimal netSales = 0.00m;
    foreach (var a in revenueAccounts)
    {
        netSales += a.Balance;
    }
    var expenseAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Expense);
    decimal totalExpenses = 0.00m;
    foreach (var a in expenseAccounts)
    {
        totalExpenses += a.Balance;
    }
    decimal netIncome = netSales - totalExpenses;
}

<div>
    <h3>IniTech</h3>
    <h4>Balance Sheet</h4>
    <p>As of @DateTime.Now.ToString("MMM ddd d HH:mm yyyy")</p>
</div>

<table class="table-condensed table">
    <tbody>
        <tr>
            <td colspan="2"><strong>Assets:</strong></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td colspan="2">Current Assets:</td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < currentAssetAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(m => currentAssetAccounts.ElementAt(i).Name)</td>
                @if (i == currentAssetAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => currentAssetAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => currentAssetAccounts.ElementAt(i).Balance)</td>
                }
            </tr>
        }
        <tr>
            <td>Total Current Assets:</td>
            <td></td>
            <td class="text-right">@(totalCurrentAssets >= 0.00m ? totalCurrentAssets.ToString("N") : "(" + (totalCurrentAssets * -1).ToString("N") + ")")</td>
        </tr>
        <tr>
            <td colspan="2">Long Term Assets:</td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < longAssetAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(m => longAssetAccounts.ElementAt(i).Name)</td>
                @if (i == longAssetAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => longAssetAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => longAssetAccounts.ElementAt(i).Balance)</td>
                }
            </tr>
        }
        <tr>
            <td>Total Long Term Assets:</td>
            <td></td>
            <td class="text-right underline">@(totalLongAssets >= 0.00m ? totalLongAssets.ToString("N") : "(" + (totalLongAssets * -1).ToString("N") + ")")</td>
        </tr>
        <tr class="active">
            <td>Total Assets:</td>
            <td></td>
            <td class="text-right"><span class="doubleUnderline">@((totalCurrentAssets + totalLongAssets).ToString("N"))</span></td>
        </tr>
        <tr>
            <td><strong>Liabilities:</strong></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td colspan="2">Current Liabilities:</td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < currentLiabilityAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(m => currentLiabilityAccounts.ElementAt(i).Name)</td>
                @if (i == currentLiabilityAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => currentLiabilityAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => currentLiabilityAccounts.ElementAt(i).Balance)</td>
                }
            </tr>
        }
        <tr>
            <td>Total Current Liabilities:</td>
            <td></td>
            <td class="text-right">@(totalCurrentLiabilities >= 0.00m ? totalCurrentLiabilities.ToString("N") : "(" + (totalCurrentLiabilities * -1).ToString("N") + ")")</td>
        </tr>
        <tr>
            <td colspan="2">Long Term Liabilities:</td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < longLiabilityAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(m => longLiabilityAccounts.ElementAt(i).Name)</td>
                @if (i == longLiabilityAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => longLiabilityAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => longLiabilityAccounts.ElementAt(i).Balance)</td>
                }
            </tr>
        }
        <tr>
            <td>Total Long Term Liabilities:</td>
            <td></td>
            <td class="underline text-right">@(totalLongLiabilities >= 0.00m ? totalLongLiabilities.ToString("N") : "(" + (totalLongLiabilities * -1).ToString("N") + ")")</td>
        </tr>
        <tr class="active">
            <td>Total Liabilities:</td>
            <td></td>
            <td class="text-right">@((totalCurrentLiabilities + totalLongLiabilities).ToString("N"))</td>
        </trclas>
        <tr>
            <td><strong>Equities:</strong></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td colspan="2">Equities:</td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < equityAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@Html.DisplayFor(m => equityAccounts.ElementAt(i).Name)</td>
                <td class="text-right">@Html.DisplayFor(m => equityAccounts.ElementAt(i).Balance)</td>
            </tr>
        }
        <tr>
            <td></td>
            <td>Retained Earnings</td>
            <td class="text-right underline">@(netIncome.ToString("N"))</td>
        </tr>
        <tr class="active">
            <td>Total Equities:</td>
            <td></td>
            <td class="text-right">@((totalEquities + netIncome).ToString("N"))</td>
        </tr>
        <tr>
            <td><strong>Total Liabilities and Equities:</strong></td>
            <td></td>
            <td class="text-right"><span class="doubleUnderline">$@((totalEquities + netIncome + totalCurrentLiabilities + totalLongLiabilities).ToString("N"))</span></td>
        </tr>
    </tbody>
</table>

<hr />

@Html.ActionLink("Back To List", "Index")
    